set dotenv-load := true
set shell := ["powershell.exe", "-c"]

build:
    echo "Building the LifeTrack API..."
    go build -o ./target/lifetrack-api ./cmd/main.go
    echo "Build completed."

dev:
    echo "Starting the LifeTrack API in development mode..."
    air

# Goose migrate up
migrate-up:
    @echo "Running migrations UP..."
    goose -dir db/migrations postgres "postgres://{{env('DB_USER')}}:{{env('DB_PASSWORD')}}@{{env('DB_HOST')}}:{{env('DB_PORT')}}/{{env('DB_NAME')}}?sslmode=disable" up

# Goose migrate down
migrate-down:
    @echo "Running migrations DOWN..."
    goose -dir db/migrations postgres "postgres://{{env('DB_USER')}}:{{env('DB_PASSWORD')}}@{{env('DB_HOST')}}:{{env('DB_PORT')}}/{{env('DB_NAME')}}?sslmode=disable" down

# Goose create migration
# Usage: just migrate-create <name>
migrate-create name:
    goose -dir db/migrations create {{name}} sql

# Create and run postgres container
pg-create:
    @echo "Starting Postgres container..."
    docker run -d \
        --name lifetrack-db \
        -e POSTGRES_USER="{{env('DB_USER')}}" \
        -e POSTGRES_PASSWORD="{{env('DB_PASSWORD')}}" \
        -e POSTGRES_DB="{{env('DB_NAME')}}" \
        -p {{env('DB_PORT')}}:5432 \
        -v lifetrack-pgdata:/var/lib/postgresql/data \
        -v {{justfile_directory()}}/db/init:/docker-entrypoint-initdb.d \
        postgres:17


    @echo "Postgres container started."

# Stop and remove Postgres container and volume
pg-delete:
    @echo "Stopping and removing Postgres container and volume..."
    -docker stop lifetrack-db
    -docker rm lifetrack-db
    -docker volume rm lifetrack-pgdata

# Recreate Postgres container
pg-recreate:
    @echo "Recreating Postgres container..."
    just pg-delete
    just pg-create

# Execute command in Postgres container
pg-exec command:
    @echo "Executing '{{command}}' in the Postgres container..."
    docker exec -it lifetrack-db {{command}}


# Enter PSQL client
pg-psql:
    just pg-exec "psql -U {{env('DB_USER')}} -d {{env('DB_NAME')}}"